package com.smartmail.receiving;

import javax.mail.internet.MimeMessage;

import android.content.Context;

import com.smartmail.accounts.MailAccount;

/**
 * 
 * @author Tomek
 *
 * This abstract class if a framework for two classes -
 * 
 * - IMAPMessageHandler 	IMAP protocol
 * - POP3MessageHandler		POP3 protocol
 * 
 * which are responsible for handling new messages from mailbox.
 */
public abstract class AbstractMessageHandler {
	
	/**
	 * Public constructor.
	 * 
	 * @param context - application Context object
	 * @param mailAccount - Messages from this mailbox account will be handled
	 */
	public AbstractMessageHandler(Context context, MailAccount mailAccount){
		
		_context = context;
		_mailAccount = mailAccount;
	}
	
	/**
	 * This method handles one MimeMessage object (mail message) from the Inbox.
	 * Its action is dependent on the protocol used to receive messages.
	 * 
	 * @param mimeMessage - message object which will be handled
	 * @param messageUID -  UID field of message
	 * @return - one of the four fields :
	 * - HANDLE_ERROR 					- for failure purposes
	 * - HANDLE_NEW_MESSAGE 				- when mimeMessage is new, and info about is saved in device
	 * - HANDLE_NEW_MESSAGE_DOWNLOADED	- when mimeMessage is new, and is completely downloaded to device
	 * - HANDLE_KNOWN_MESSAGE 			- when mimeMessage is already stored in device
	 */
	public abstract int handle(MimeMessage mimeMessage, String messageUID);
	
	/**
	 * The method used to download the complete message from the server.
	 * This method is only used for the POP3 protocol.
	 * 
	 * @param mimeMessage - message object wanted to be downloaded
	 * @return - one of the three fields :
	 * - HANDLE_ERROR 					- for failure purposes
	 * - HANDLE_NEW_MESSAGE_DOWNLOADED	- when mimeMessage is new, and is completely downloaded to device
	 * - HANDLE_KNOWN_MESSAGE 			- when mimeMessage is already stored in device
	 */
	public abstract int download(MimeMessage mimeMessage);
	
	
	/**
	 * log tag for logging purposes
	 */
	protected static String _logTag = "com.smartmail.receiving";
	
	/**
	 * values ​​which are returned as the result of the methods
	 * - handle
	 * - download
	 */
	public static final int HANDLE_ERROR = 0;
	public static final int HANDLE_NEW_MESSAGE = 1;
	public static final int HANDLE_KNOWN_MESSAGE = 2;
	public static final int HANDLE_NEW_MESSAGE_DOWNLOADED = 3;
	
	/**
	 * Application context, for sending intents.
	 */
	protected Context _context;
	
	/**
	 * Mailbox account. Messages from this mailbox account will be handled.
	 */
	protected MailAccount _mailAccount;

}
